import Vue from 'vue'
import VueRouter from 'vue-router'
import Index from '../views/Index.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Index',
    component: Index,
    meta: {
      title: 'message.term.index',
    },
    children: [{
      path: '/court-address',
      name: 'CourtAddress',
      meta: {
        title: 'message.term.court_address',
      },
      component: () => import('@/views/court-address/CourtAddress.vue')
    },{
      path: '/widget',
      name: 'widget',
      meta: {
        title: 'message.term.widget',
      },
      component: () => import('@/views/widget/widget.vue')
    }]
  },
  {
    path: '/about',
    name: 'About',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

/**
 * 当跳转页面的时候，把还在等待提交的请求全部终止，有利于提升性能
 */
router.beforeEach((to, from, next) => {
  // 切换路由时清空上个路由未完成的所有请求
  if (Vue.prototype.__axiosPromiseRequestArray) {
    Vue.prototype.__axiosPromiseRequestArray.forEach((cancel) => {
      cancel();
    });
    Vue.prototype.__axiosPromiseRequestArray.length = 0;
  }

  next();
})

export default router
